package com.ceba.test.modules.signvisual.logicService.abstracts;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.math.NumberUtils;

import com.ceba.test.base.entity.SignPrintPDF;
import com.ceba.test.modules.admin.entity.GStamp;
import com.ceba.test.modules.admin.service.StampService;
import com.ceba.test.modules.signvisual.service.SignPrintPdfComparator;
import com.ceba.test.modules.statistic.entity.ReportSign;

/**
 * 可视化签章，签名接口
 * @author Administrator
 */
public abstract class SignVisualAbstract {
	
	/**
	 * 盖章
	 * @param signPrintPDFList 用户章 信息
	 * @param pdfPath 盖章报告
	 * @return 盖章后生成的报告路径 --临时路径
	 */
	public abstract String seal(List<SignPrintPDF> signPrintPDFList,String pdfPath,String outPdfPath) throws Exception;
	
	/**
	 * 签名
	 * @param signPrintPDFList 用户章 信息
	 * @param pdfPath 签名报告
	 * @param parm 
	 * @return 签名后生成的报告路径 --临时路径
	 */
	public abstract String autograph(List<SignPrintPDF> signPrintPDFList,String pdfPath,String outPdfPath, ReportSign parm) throws Exception;
	
	/**
	 * signPrintPdf转换为g_stamp
	 * @param signPrintPdfList
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<GStamp> signPrintPDFToG_Stamp(List<SignPrintPDF> signPrintPdfList){
		if(CollectionUtils.isEmpty(signPrintPdfList)){
			return null;
		}
		
		List<GStamp> g_StampList=new ArrayList();
		
		SignPrintPDF[] signPrintPDFs=new SignPrintPDF[signPrintPdfList.size()];
		
		for(int index=0;index<signPrintPdfList.size();index++){
			signPrintPDFs[index]=signPrintPdfList.get(index);
		}
		//打印层排序
		Arrays.sort(signPrintPDFs,new SignPrintPdfComparator());
		for(int index=0;index<signPrintPDFs.length;index++){
			SignPrintPDF signPrintPDF=signPrintPDFs[index];
			GStamp g_stamp=new GStamp();
			g_stamp.setId(signPrintPDF.getId());
			g_stamp.setUserId(signPrintPDF.getUserid());
			g_stamp.setStamp_x(signPrintPDF.getPdf_x());
			g_stamp.setStamp_y(signPrintPDF.getPdf_y());
			g_stamp.setStampPage(signPrintPDF.getPage());	
			float imgWidth=0,imgHeight=0;
			if(!signPrintPDF.getIsZoomRate()){
				imgWidth = signPrintPDF.getImageWidth()*NumberUtils.toFloat(signPrintPDF.getZoomRate(),1);
				imgHeight = signPrintPDF.getImageHeight()*NumberUtils.toFloat(signPrintPDF.getZoomRate(),1);
			}else{
				imgWidth = signPrintPDF.getImageWidth();
				imgHeight = signPrintPDF.getImageHeight();
			}
			g_stamp.setImgHeight(ObjectUtils.toString(imgHeight));
			g_stamp.setImgWidth(ObjectUtils.toString(imgWidth));
			g_stamp.setFilePath(signPrintPDF.getImagePath());
			g_StampList.add(g_stamp);
		}
		return g_StampList;
	}


	/**
	 * 与业务关联获取签章数据
	 * @param signPrintPdfList
	 * @throws Exception
	 */
	public void signPrintPDFToIdG_Stamp(List<SignPrintPDF> signPrintPdfList) throws Exception{
		if(CollectionUtils.isEmpty(signPrintPdfList)){
			return;
		}
		StampService stampService=new StampService();
		for(SignPrintPDF signPrintPDF : signPrintPdfList){
			GStamp g_stamp = stampService.getStampById(signPrintPDF.getId());
			if(!signPrintPDF.getIsZoomRate()){
				signPrintPDF.setImageHeight(NumberUtils.toFloat(g_stamp.getImgHeight()));
				signPrintPDF.setImageWidth(NumberUtils.toFloat(g_stamp.getImgWidth()));
			}
			signPrintPDF.setImagePath(g_stamp.getFilePath());
		}
	}
}
